package com.csweimei.rentmanagementtreasure;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;
import android.view.KeyEvent;

import com.csweimei.rentmanagementtreasure.base.AppViewModelFactory;
import com.csweimei.rentmanagementtreasure.databinding.ActivityMainBinding;
import com.csweimei.rentmanagementtreasure.tenant.ui.fragment.TabFourFragment;
import com.csweimei.rentmanagementtreasure.tenant.ui.fragment.TabOneFragment;
import com.csweimei.rentmanagementtreasure.tenant.ui.fragment.TabThreeFragment;
import com.csweimei.rentmanagementtreasure.tenant.ui.fragment.TabTwoFragment;
import com.csweimei.rentmanagementtreasure.viewModel.LoginVM;
import com.csweimei.rentmanagementtreasure.viewModel.MainVm;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class MainActivity extends BaseActivity<ActivityMainBinding, MainVm> {

    private FragmentManager mFragmentManager;

    private static final String TAG_1 = "TAG_1";
    private static final String TAG_2 = "TAG_2";
    private static final String TAG_3 = "TAG_3";
    private static final String TAG_4 = "TAG_4";

    private TabOneFragment tabOneFragment;
    private TabTwoFragment tabTwoFragment;
    private TabThreeFragment tabThreeFragment;
    private TabFourFragment tabFourFragment;

    private int mCurrentItem = 0;


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        mFragmentManager = getSupportFragmentManager();
        //初始化Fragment
        initFragment();
        //初始化底部Button
        initBottomClick();

        showFragment(mCurrentItem);
        viewModel.setActivity(this);

    }

    @Override
    public MainVm initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(MainVm.class);
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewModel.onResume();
    }

    private void initBottomClick() {
        binding.tab1.setOnClickListener(v -> {
            mCurrentItem = 0;
            showFragment(mCurrentItem);
        });
        binding.tab2.setOnClickListener(v -> {
            mCurrentItem = 1;
            showFragment(mCurrentItem);
        });
        binding.tab3.setOnClickListener(v -> {
            mCurrentItem = 2;
            showFragment(mCurrentItem);
        });
        binding.tab4.setOnClickListener(v -> {
            mCurrentItem = 3;
            showFragment(mCurrentItem);
        });
    }


    private void initFragment() {
        tabOneFragment = new TabOneFragment();
        tabTwoFragment = new TabTwoFragment();
        tabThreeFragment = new TabThreeFragment();
        tabFourFragment = new TabFourFragment();
    }

    private void showFragment(int index) {
//        if (index == 1){
//            if (!UserUtils.isLogin()) {
//                startActivity(LoginActivity.class);
//            }
//        }

        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        setTextViewDefaultColor();
        switch (index) {
            case 0:
                binding.tvTab1.setTextColor(getResources().getColor(R.color.tab_s));
                binding.ivTab1.setImageResource(R.mipmap.tab_1_s);
                if (tabOneFragment != null && !tabOneFragment.isAdded()) {
                    addFragmentToActivityWithTag(mFragmentManager, tabOneFragment, R.id.ll_frame, TAG_1);
                }
                hideAllFragment(transaction);
                transaction.show(tabOneFragment);
                break;
            case 1:
                binding.tvTab2.setTextColor(getResources().getColor(R.color.tab_s));
                binding.ivTab2.setImageResource(R.mipmap.tab_2_s);
                if (tabTwoFragment != null && !tabTwoFragment.isAdded()) {
                    addFragmentToActivityWithTag(mFragmentManager, tabTwoFragment, R.id.ll_frame, TAG_2);
                }
                hideAllFragment(transaction);
                transaction.show(tabTwoFragment);
                break;
            case 2:
                binding.ivTab3.setImageResource(R.mipmap.tab_3_s);
                binding.tvTab3.setTextColor(getResources().getColor(R.color.tab_s));
                if (tabThreeFragment != null && !tabThreeFragment.isAdded()) {
                    addFragmentToActivityWithTag(mFragmentManager, tabThreeFragment, R.id.ll_frame, TAG_3);
                }
                hideAllFragment(transaction);
                transaction.show(tabThreeFragment);
                break;
            case 3:
                binding.ivTab4.setImageResource(R.mipmap.tab_4_s);
                binding.tvTab4.setTextColor(getResources().getColor(R.color.tab_s));
                if (tabFourFragment != null && !tabFourFragment.isAdded()) {
                    addFragmentToActivityWithTag(mFragmentManager, tabFourFragment, R.id.ll_frame, TAG_4);
                }
                hideAllFragment(transaction);
                transaction.show(tabFourFragment);
                break;
        }

        transaction.commit();
    }

    private void setTextViewDefaultColor() {
        //所有文本先设置回默认颜色
        binding.tvTab1.setTextColor(getResources().getColor(R.color.text_c));
        binding.tvTab2.setTextColor(getResources().getColor(R.color.text_c));
        binding.tvTab3.setTextColor(getResources().getColor(R.color.text_c));
        binding.tvTab4.setTextColor(getResources().getColor(R.color.text_c));

        binding.ivTab1.setImageResource(R.mipmap.tab_1_h);
        binding.ivTab2.setImageResource(R.mipmap.tab_2_h);
        binding.ivTab3.setImageResource(R.mipmap.tab_3_h);
        binding.ivTab4.setImageResource(R.mipmap.tab_4_h);
    }

    private void hideAllFragment(FragmentTransaction transaction) {
        transaction
                .hide(tabOneFragment)
                .hide(tabTwoFragment)
                .hide(tabThreeFragment)
                .hide(tabFourFragment);

    }

    /**
     * 把Fragment添加到Activity上
     *
     * @param manager         FragmentManager的实例
     * @param fragment        Fragment的实例
     * @param containerViewId 布局id
     * @param tag             索引
     */
    private void addFragmentToActivityWithTag(FragmentManager manager, Fragment fragment,
                                              int containerViewId, String tag) {
        if (manager == null || fragment == null || tag == null || fragment.isAdded()) {

            return;
        }
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(containerViewId, fragment, tag);
        transaction.commit();
    }


    //记录用户首次点击返回键的时间
    private long firstTime = 0;

    /**
     * 第一种解决办法 通过监听keyUp
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 2000) {
                ToastUtils.showShort("再按一次退出程序");
                firstTime = secondTime;
                return true;
            } else {
                finish();
            }
        }

        return super.onKeyUp(keyCode, event);
    }
}